import java.util.Arrays;

class Solution {
    public int removeDuplicates(int[] nums) {
        int ret = 0;
        for(int left = 0,right = 0;right < nums.length;){
            int count = 0;
            while(nums[right] == nums[left]) {
                count ++;
                if(count > 2){
                    ret ++;
                    nums[right] = Integer.MAX_VALUE;


                }
                right ++;
                if(right == nums.length){
                    break;
                }
            }
            left = right;
        }
        Arrays.sort(nums);
        int ans = nums.length - ret;
        return ans;
    }
}